// pages/order/order.js
import tips from '../../lib/tips';
var util = require('../../utils/util.js');
const app = getApp();
var tPageNo = 1;
var tHasNextPage = false;
var oPageNo = 1;
var oHasNextPage = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('view.open-type.getUserInfo'),
    baseDomain: app.globalData.baseDomain,
    apiDomain: app.globalData.apiDomain,
    transfer: null,
    orders: null,
    tipsData: {
      title: '',
      isHidden: true
    },
    noResult: "没有要展示的内容"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (app.globalData.userInfo) {
      //调用获取帮转列表接口
      wx.request({
        url: app.globalData.apiDomain + '/api/transfer/getTransferByUser',
        method: "POST",
        data: {
          openId: app.globalData.openId,
          pageNo: 1
        },
        success: function(res) {
          tHasNextPage = res.data.data.hasNextPage;
          that.setData({
            transfer: res.data.data.list,
          });
        },
        complete: function() {
          if (tHasNextPage) {
            tPageNo = 2;
          }
        }
      });
      //调用获取订单列表接口
      wx.request({
        url: app.globalData.apiDomain + '/api/order/getOrderBySaler',
        method: "POST",
        data: {
          openId: app.globalData.openId,
          pageNo: 1
        },
        success: function(res) {
          oHasNextPage = res.data.data.hasNextPage;
          that.setData({
            orders: res.data.data.list,
          });
        },
        complete: function() {
          if (oHasNextPage) {
            oPageNo = 2;
          }
        }
      });
    } else {
      wx.switchTab({
        url: '../userCenter/userCenter',
        complete: function() {
          wx.showToast({
            title: '请先登录',
            image: '../../images/failure_toast.png',
            duration: 2000,
          });
        }
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //调用获取帮转列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/transfer/getTransferByUser',
      method: "POST",
      data: {
        openId: app.globalData.openId,
        pageNo: 1
      },
      success: function(res) {
        tHasNextPage = res.data.data.hasNextPage;
        that.setData({
          transfer: res.data.data.list,
        });
      },
      complete: function() {
        if (tHasNextPage) {
          tPageNo = 2;
        }
      }
    });

    //调用获取订单列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/order/getOrderBySaler',
      method: "POST",
      data: {
        openId: app.globalData.openId,
        pageNo: 1
      },
      success: function(res) {
        oHasNextPage = res.data.data.hasNextPage;
        that.setData({
          orders: res.data.data.list,
        });
      },
      complete: function() {
        if (oHasNextPage) {
          oPageNo = 2;
        }
      }
    });
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (!tHasNextPage && !oHasNextPage) {
      return;
    }
    //调用获取帮转列表接口
    if (tHasNextPage) {
      wx.request({
        url: app.globalData.apiDomain + '/api/transfer/getTransferByUser',
        method: "POST",
        data: {
          openId: app.globalData.openId,
          pageNo: tPageNo
        },
        success: function(res) {
          tHasNextPage = res.data.data.hasNextPage;
          var oldTransfer = that.data.transfer;
          var list = res.data.data.list;
          for (var i = 0; i < list.length; i++) {
            oldTransfer.push(list[i]);
          }
          that.setData({
            transfer: oldTransfer,
          });
        },
        complete: function() {
          if (tHasNextPage) {
            tPageNo++;
          }
        }
      });
    }
    //调用获取订单列表接口
    if (oHasNextPage) {
      wx.request({
        url: app.globalData.apiDomain + '/api/order/getOrderBySaler',
        method: "POST",
        data: {
          openId: app.globalData.openId,
          pageNo: oPageNo
        },
        success: function(res) {
          oHasNextPage = res.data.data.hasNextPage;
          var oldOrders = that.data.orders;
          var list = res.data.data.list;
          for (var i = 0; i < list.length; i++) {
            oldOrders.push(list[i]);
          }
          that.setData({
            orders: oldOrders
          });
        },
        complete: function() {
          if (oHasNextPage) {
            oPageNo++;
          }
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  sendGoods: util.throttle(function(e) {
    var transferId = e.currentTarget.dataset.transferId;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../inputPostNumber/inputPostNumber?transferId=' + transferId + '&id=' + id,
    })
  }),
  queryExpress: util.throttle(function(e) {
    var postNum = e.currentTarget.dataset.expressNum;
    wx.navigateTo({
      url: '../expressInfo/expressInfo?expressNum=' + postNum,
    })
  }),

  showToast(title, duartion) {
    const that = this;
    const tipsData = {
      title: title || '',
      duartion: duartion || 2000,
      isHidden: false
    };
    tips.toast(that.data.tipsData);
    that.setData({
      tipsData
    });
    setTimeout(() => {
      tipsData.isHidden = true;
      that.setData({
        tipsData
      });
    }, tipsData.duartion);
  },

  refresh: util.throttle(function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.apiDomain + '/api/transfer/refresh/' + id,
      method: "GET",
      success: function(e) {
        console.log(e.data.resCode);
        if (e.data.resCode == 0) {
          wx.showToast({
            title: '刷新成功',
            image: '../../images/correct_toast.png',
            duration: 2000,
          });
        }
        if (e.data.resCode == 1) {
          that.showToast(e.data.resMsg, 2000);
        }
      }
    })
  })
})